package controller

import (
	"haiqiu-go-admin/result"
	"haiqiu-go-admin/service"
	"haiqiu-go-admin/utils"
	"net/http"
)

func (*MenuApi) MenuHandle(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodGet:
		MenuAPI.menuById(w, r)
		break
	case http.MethodPost:
		MenuAPI.menuAdd(w, r)
		break
	case http.MethodPut:
		MenuAPI.menuUpdate(w, r)
		break
	case http.MethodDelete:
		MenuAPI.menuDeleteBatch(w, r)
		break
	default:
		break
	}
}

func (*MenuApi) Menu(w http.ResponseWriter, r *http.Request) {
	_, _, username, err := utils.Token{}.GetUserTokenAndUserIdAndUsername(w, r)
	menu, err := service.MenuRouter(*username)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, menu)
}

// 根据ID查询数据
func (*MenuApi) menuById(w http.ResponseWriter, r *http.Request) {
	page, err := service.MenuById(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// MenuPage 分页查询
func (*MenuApi) MenuPage(w http.ResponseWriter, r *http.Request) {
	page, err := service.MenuPage(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// 新增数据
func (*MenuApi) menuAdd(w http.ResponseWriter, r *http.Request) {
	add, err := service.MenuAdd(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 批量删除数据
func (*MenuApi) menuDeleteBatch(w http.ResponseWriter, r *http.Request) {
	add, err := service.MenuDeleteBatch(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 根据ID更新数据
func (*MenuApi) menuUpdate(w http.ResponseWriter, r *http.Request) {
	add, err := service.MenUpdate(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// MenuList 查询所有数据
func (*MenuApi) MenuList(w http.ResponseWriter, r *http.Request) {
	menuList, err := service.MenuList(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, menuList)
}

// 树形菜单权限列表
func (*MenuApi) MenuTree(w http.ResponseWriter, r *http.Request) {
	menuList, err := service.TreeMenu(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, menuList)
}

// 角色ID查询菜单ID集合与菜单下拉树结构
func (*MenuApi) RoleMenuTreeSelect(w http.ResponseWriter, r *http.Request) {
	mapData, err := service.RoleMenuTreeSelect(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, mapData)
}
